#include <bits/stdc++.h>
using namespace std;
const int maxn = 200+10;
string buf[maxn];
int R;
void solve(int r, int c){
    cout << buf[r][c] << "(";
    if(r+1<R && buf[r+1][c] == '|'){
        int r1 = r+2, c1 = c, c2 = c;
        while(c1-1>=0 && buf[r1][c1-1]=='-') --c1;
        while(c2+1<buf[r1].size() && buf[r1][c2+1]=='-') ++c2;
        if(r1+1<R){
            for (r1+=1; c1 <= c2 && c1<buf[r1].size(); ++c1) {
                if(!isspace(buf[r1][c1])) solve(r1, c1);
            }
        }
    }
    cout << ")";
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    int T; cin >> T; cin.get();
    while(T--){
        R = 0;
        while(getline(cin, buf[R])){
            if(buf[R][0] == '#') break; else ++R;
        }
        cout << "(";
        if(R){
            for (int c = 0; c < buf[0].size(); ++c) {
                if(!isspace(buf[0][c])) solve(0, c);
            }
        }
        cout << ")\n";
    }
    return 0;
}